//
//  HYOUSearchView.m
//  BestGift
//
//  Created by 温晓宇 on 16/1/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HYOUSearchView.h"
#import "HYOUSearchViewCell.h"
@interface HYOUSearchView ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>


//@property(nonatomic,retain)NSMutableArray *allArray;

@property(nonatomic,retain)HYOUBaseTableView *myTableView;


@property(nonatomic,assign)NSInteger myIndex;

@property(nonatomic,retain)UISearchBar *mySearchBar;
//@property(nonatomic,retain)UISearchDisplayController *searchDC;


@end

@implementation HYOUSearchView
//-(void)dealloc{
//    [_searchController release];
//    [_searchArray release];
//    [_delegate release];
//    [_myTableView release];
//    [_mySearchBar release];
//    [super dealloc];
//}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}
-(void)createSubViews{
//    self.allArray = [NSMutableArray array];
    self.searchArray = [NSMutableArray array];
    
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
//    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
    self.searchController.searchBar.delegate = self;
    [self.searchController.searchBar sizeToFit];
//    [_searchController release];
    
    self.myTableView = [[HYOUBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.tableHeaderView = self.searchController.searchBar;
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView registerClass:[HYOUSearchViewCell class] forCellReuseIdentifier:@"active"];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [_myTableView release];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    if (section == 0) {
//        return 1;
//    }else{
        return self.stringArray.count;
//    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


//    if (self.searchController.active) {
//        HYOUSearchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"active"];
//        cell.bigArray = self.searchArray;
//        NSLog(@"%ld", self.searchArray.count);
//        return cell;
//    }else{
//    if (indexPath.section == 1) {
        HYOUSearchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"active"];
        NSString *name = [self.stringArray objectAtIndex:indexPath.row];
        NSLog(@"%@",name);
        cell.name = name;
        //    cell.textLabel.text = name;
        return cell;
//    }else{
//        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//        cell.textLabel.text = @"热门搜索";
//        return cell;
//    }
    
    
//    }

}
//-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
//    NSString *searchString = [self.searchController.searchBar text];
//    NSLog(@"===%@",searchString);
//    if ([searchString isEqualToString:@""]) {
//        
//    }else{
//    [self.delegate searchString:searchString];
//    if (self.searchArray!= nil) {
//        [self.searchArray removeAllObjects];
//    }
////    NSString *url = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/search/item?keyword=%@&limit=20&offset=0&sort=",searchString];
////    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
////    NSLog(@"%@", url);
////    [AppTools finalAFNWithURLString:url parameters:nil successAFNBlock:^(id resuposeObject) {
////        NSMutableDictionary *bigDic = resuposeObject;
////        NSMutableDictionary *data = [bigDic objectForKey:@"data"];
////        NSMutableArray *items = [data objectForKey:@"items"];
////        for (NSDictionary *dic in items) {
////            HYOUHotModel *model = [[HYOUHotModel alloc] init];
////            [model setValuesForKeysWithDictionary:dic];
////            [self.searchArray addObject:model];
////            NSLog(@"%@",model.name);
////        }
////    }];
//        [self.myTableView reloadData];
//    }
//}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSString *searchString = [self.searchController.searchBar text];
    NSLog(@"===%@",searchString);
        [self.delegate searchString:searchString];
        if (self.searchArray!= nil) {
            [self.searchArray removeAllObjects];
        }
        //    NSString *url = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/search/item?keyword=%@&limit=20&offset=0&sort=",searchString];
        //    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        //    NSLog(@"%@", url);
        //    [AppTools finalAFNWithURLString:url parameters:nil successAFNBlock:^(id resuposeObject) {
        //        NSMutableDictionary *bigDic = resuposeObject;
        //        NSMutableDictionary *data = [bigDic objectForKey:@"data"];
        //        NSMutableArray *items = [data objectForKey:@"items"];
        //        for (NSDictionary *dic in items) {
        //            HYOUHotModel *model = [[HYOUHotModel alloc] init];
        //            [model setValuesForKeysWithDictionary:dic];
        //            [self.searchArray addObject:model];
        //            NSLog(@"%@",model.name);
        //        }
        //    }];
        [self.myTableView reloadData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *name = [self.stringArray objectAtIndex:indexPath.row];
    NSLog(@"%@",name);
    [self.delegate searchString:name];
}
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
////    if (self.searchController.active) {
//////        return HEIGHT;
////        NSLog(@"12312312312312")
////        return self.searchArray.count * 118;
////    }else{
//        return 20;
////    }
//}
-(void)setSearchArray:(NSMutableArray *)searchArray{
    if (_searchArray != searchArray) {
        [_searchArray release];
        _searchArray = [searchArray retain];
    }
    [self.myTableView reloadData];
}
-(void)setStringArray:(NSMutableArray *)stringArray{
    if (_stringArray != stringArray) {
        [_stringArray release];
        _stringArray = [stringArray retain];
    }
    NSLog(@"%@",self.stringArray);
    [self.myTableView reloadData];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
